package com.example.demo.controller;


import com.example.demo.entity.Borrow;
import com.example.demo.service.IBorrowService;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/borrow1")
public class Import {
    @Autowired
    private IBorrowService borrowService;
    @PostMapping("/import")
    public ResponseEntity<String> importExcel(@RequestParam("file") MultipartFile file) throws IOException, ParseException {
        if (file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        List<Borrow> borrows = new ArrayList<>();
        // 遍历 Excel 行并将数据导入到应用程序中
        for (Row row : sheet) {
            //根据表头判断要保存的字段
            if (row.getRowNum() == 0) {
                continue;
            }
            // 读取 Excel 行中的数据
            int userIdStr = (int) row.getCell(0).getNumericCellValue();
            int bookIdStr = (int) row.getCell(1).getNumericCellValue();
            Date borrowDate;
            Date dueDate;
            Date returnDate;
            if (row.getCell(2).getCellType() == CellType.NUMERIC) {
                borrowDate = row.getCell(2).getDateCellValue();
            } else {
                String borrowDateStr = row.getCell(2).getStringCellValue();
                borrowDate = new SimpleDateFormat("yyyy-MM-dd").parse(borrowDateStr);
            }
            if (row.getCell(3).getCellType() == CellType.NUMERIC) {
                dueDate = row.getCell(3).getDateCellValue();
            } else {
                String dueDateStr = row.getCell(3).getStringCellValue();
                dueDate = new SimpleDateFormat("yyyy-MM-dd").parse(dueDateStr);
            }
            if (row.getCell(4).getCellType() == CellType.NUMERIC) {
                returnDate = row.getCell(4).getDateCellValue();
            } else {
                String returnDateStr = row.getCell(4).getStringCellValue();
                returnDate = new SimpleDateFormat("yyyy-MM-dd").parse(returnDateStr);
            }
            String status = row.getCell(5).getStringCellValue();
            String userMajor = row.getCell(6).getStringCellValue();
            int userGradeStr = (int) row.getCell(7).getNumericCellValue();

            // 进行数据清洗
            Integer userId;
            Integer bookId;
            Integer userGrade;

            try {
                userId = userIdStr;
                bookId = bookIdStr;
                userGrade = userGradeStr;
            } catch (NumberFormatException e) {
                // 如果数据不符合预期，忽略该行
                continue;
            }

            // 创建借阅记录对象并添加到列表中
            Borrow borrow = new Borrow();
            borrow.setUserId(userId);
            borrow.setBookId(bookId);
            borrow.setBorrowDate(borrowDate);
            borrow.setDueDate(dueDate);
            borrow.setReturnDate(returnDate);
            borrow.setStatus(status);
            borrow.setUserMajor(userMajor);
            borrow.setUserGrade(userGrade);
            borrows.add(borrow);
        }
        // 将数据保存到 borrow 表中
        List<Borrow> savedBorrows = borrowService.saveAll(borrows);
        if (savedBorrows.size() == borrows.size()) {
            return ResponseEntity.ok("Excel 导入成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel 导入失败");
        }
    }



}
